import java.util.concurrent.CountDownLatch;

public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(3);
        Thread t1 = new Thread(()->{
            for(int i=0;i<3;i++){
                try {
                    Thread.sleep((long) (Math.random() * 2000));
                    latch.countDown();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        latch.await(); // 阻塞主线程，直到计数器为 0
        System.out.println("所有任务执行完毕");
    }
}
